package net.moonbiter.ebs;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import java.util.List;

public class ServiceDef {
	private Map<String,InputDef> inputs;
	private Method serviceMethod; 
	private Object serviceInstance;
	private List<String> orderedInputNamesList;
	
	public ServiceDef(Object inst, Method srvMet){
		this.serviceInstance = inst;
		this.serviceMethod = srvMet;
		inputs = new HashMap<String,InputDef>();
		orderedInputNamesList = new LinkedList<String>();
	}
	
	public Map<String,InputDef> getInputs(){
		return inputs;
	}
	
	public void addInput(String name, InputDef def){
		inputs.put(name, def);
		orderedInputNamesList.add(name);
	}

	public Method getServiceMethod() {
		return serviceMethod;
	}
	
	public InputDef getInputDef(String name){
		if (!inputs.containsKey(name)){
			throw new IllegalArgumentException();
		}
		return inputs.get(name);
	}

	public Object getServiceInstance() {
		return serviceInstance;
	}

	public String[] getOrderedInputNames() {
		return orderedInputNamesList.toArray(new String[]{});
	}
	
	public Class<?>[] getDeclaredExceptions(){
		return serviceMethod.getExceptionTypes();
	}
	
	public Class<?> getReturnType(){
		return serviceMethod.getReturnType();
	}
	
	public Map<String,Class<?>> getInputTypes(){
		Map<String,Class<?>> inputTypes = new HashMap<String,Class<?>>();
		for (String inputName : getOrderedInputNames()){
			inputTypes.put(inputName, getInputDef(inputName).getType());
		}
		return inputTypes;
	}
}
